package com.skp.shaphan.ui.connectioncontrols;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Properties;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.skp.shaphan.actions.JTDSConnectAction;
import com.skp.shaphan.ui.ActionBasedPanel;
import com.skp.shaphan.ui.ConnectRunnable;

public class JTDSConnectionOptions extends JPanel implements ActionBasedPanel, ConnectionOptions {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2289701325448216193L;
	private JComboBox serverType;
	private JComboBox serverVersion;
	private JTextField serverName;
	private JTextField portNumber;
	private JTextField userName;
	private JTextField databaseName;
	private JPasswordField password;
	private JTDSConnectAction action;
	
	public JTDSConnectionOptions() {
		this.setLayout(new GridBagLayout());
		JLabel label;
		int row = 0;
		
		label = new JLabel("Type: ");
		this.add(label, new GridBagConstraints(0, row, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 5, 5));
		serverType = new JComboBox(new Object[]{"Sybase", "SQL Server"});
		serverType.setEditable(false);
		this.add(serverType, new GridBagConstraints(1, row++, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 5, 5));

		label = new JLabel("Version: ");
		this.add(label, new GridBagConstraints(0, row, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 5, 5));
		serverVersion = new JComboBox(new Object[]{"default", "4.2", "5.0", "7.0", "8.0"});
		serverVersion.setEditable(false);
		this.add(serverVersion, new GridBagConstraints(1, row++, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 5, 5));

		label = new JLabel("Server Name: ");
		this.add(label, new GridBagConstraints(0, row, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 5, 5));
		serverName = new JTextField();
		this.add(serverName, new GridBagConstraints(1, row++, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 5, 5));

		label = new JLabel("Port Number: ");
		this.add(label, new GridBagConstraints(0, row, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 5, 5));
		portNumber = new JTextField("");
		this.add(portNumber, new GridBagConstraints(1, row++, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 5, 5));

		label = new JLabel("Database Name: ");
		this.add(label, new GridBagConstraints(0, row, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 5, 5));
		databaseName = new JTextField("");
		this.add(databaseName, new GridBagConstraints(1, row++, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 5, 5));

		label = new JLabel("User Name");
		this.add(label, new GridBagConstraints(0, row, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 5, 5));
		userName = new JTextField();
		this.add(userName, new GridBagConstraints(1, row++, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 5, 5));

		label = new JLabel("Password");
		this.add(label, new GridBagConstraints(0, row, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 5, 5));
		password = new JPasswordField();
		this.add(password, new GridBagConstraints(1, row++, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 5, 5));
	}

	@Override
	public ConnectRunnable getAction() {
		if(action == null) {
			action = new JTDSConnectAction(this);
		}
		return action;
	}
	
	public String getServerName() {
		return serverName.getText();
	}
	public String getPort() {
		return portNumber.getText();
	}
	public String getUser() {
		return userName.getText();
	}
	public String getPassword() {
		return new String(password.getPassword());
	}
	
	public String getServerType() {
		if(serverType.getSelectedIndex() == 0) {
			return "sybase";
		} else {
			return "sqlserver";
		}
	}
	
	public String getDatabaseName() {
		return databaseName.getText();
	}
	
	/*
	 * from: http://jtds.sourceforge.net/faq.html#urlFormat
	 * TDS (default - "8.0" for SQL Server; "5.0" for Sybase)
The version of TDS to be used. TDS (Tabular Data Stream) is the protocol used by Microsoft SQL Server and Sybase to communicate with database clients. jTDS can use TDS 4.2, 5.0, 7.0 and 8.0. Version 4.2 is used by SQL Server 6.5 and Sybase 10. Version 5.0 is used with Sybase 11 onwards. Version 7.0 is used by SQL Server 7.0; this protocol also works with SQL Server 2000. Version 8.0 is used by SQL Server 2000 and SQL Server 2005.
Newer database server versions usually understand older protocol versions. This means that SQL Server 7.0 can be used with TDS 4.2, but the limitations of the protocol apply regardless of the server version (e.g. when using TDS 4.2 VARCHARs are limited to 255 characters). As a conclusion, you must set this property to "4.2" when connecting to SQL Server 6.5 or Sybase. You should not set this value to "7.0" or "8.0") when connecting to any version of Sybase as these are SQL Server specific protocols. Further, you should not set this value to "5.0") when connecting to any version of SQL Server as this is a Sybase specific protocol.
Currently jTDS automatically falls back from 8.0 to 7.0 (if used with SQL Server 7.0) and from 5.0 to 4.2 (with Sybase 10) so specifying the value for this parameter is only necessary for SQL Server 6.5.
	 */
	public String getVersion() {
		if(serverVersion.getSelectedIndex() == 0) {
			return "";
		}
		return serverVersion.getSelectedItem().toString();
	}

	@Override
	public void setConnectionOptions(Properties properties) {
		String serverType = properties.getProperty("serverType", "sybase");
		if(serverType.equals("sqlserver")) {
			this.serverType.setSelectedIndex(1);
		} else {
			this.serverType.setSelectedIndex(0);
		}
		password.setText(properties.getProperty("password"));
		portNumber.setText(properties.getProperty("portNumber"));
		serverName.setText(properties.getProperty("serverName"));
		userName.setText(properties.getProperty("userName"));
		databaseName.setText(properties.getProperty("databaseName"));
		String version = properties.getProperty("TDSVersion", "");
		if(version == "") {
			serverVersion.setSelectedIndex(0);
		} else {
			serverVersion.setSelectedItem(version);
		}
	}

	@Override
	public Properties getConnectionOptions() {
		Properties props = new Properties();
		if(serverType.getSelectedIndex() == 0) {
			props.setProperty("serverType", "sybase");
		} else {
			props.setProperty("serverType", "sqlserver");
		}
		props.setProperty("password", new String(password.getPassword()));
		props.setProperty("portNumber", portNumber.getText());
		props.setProperty("serverName", serverName.getText());
		props.setProperty("userName", userName.getText());
		props.setProperty("databaseName", databaseName.getText());
		props.setProperty("TDSVersion", getVersion());
		return props;
	}

	@Override
	public String getUserFriendlyName() {
		return "SQL Server";
	}

}
